package com.atwisdom.star.auth.business.Impl;

import com.atwisdom.star.auth.business.AuthBaseBslService;
import com.atwisdom.star.auth.business.RolegpBslService;
import com.atwisdom.star.auth.pojo.Rolegp;
import com.atwisdom.star.auth.service.impl.RolegpServiceImpl;
import com.atwisdom.star.common.actionType.auth.RolegpActionType;
import com.atwisdom.star.common.vo.*;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 业务处理类
 */
@Service
public class RolegpBslServiceImpl extends AuthBaseBslService<Rolegp, Rolegp> implements RolegpBslService {

    private final RolegpServiceImpl rolegpServiceImpl;

    public RolegpBslServiceImpl(RolegpServiceImpl rolegpServiceImpl) {
        this.rolegpServiceImpl = rolegpServiceImpl;
    }

    /**
     * 服务工厂方法
     *
     * @param actionDataList 参数
     * @return 参数
     * @throws BusinessException 异常
     */
    @Transactional
    public ResultInfo<?> responseFactory(List<ActionData<Rolegp>> actionDataList) throws BusinessException {
        ResultInfo<?> resultInfo = null;
        for (ActionData<Rolegp> actionData : actionDataList) {
            String action = actionData.getAction();
            RolegpActionType actionType = RolegpActionType.valueOf(action);
            String methodName = actionType.getMethodName();
            resultInfo = this.execMethod(methodName, actionData);
        }
        return resultInfo;
    }

    /**
     * 反射执行业务方法
     *
     * @param methodName 方法名称
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     * @throws BusinessException 业务数据异常
     */
    public ResultInfo<?> execMethod(String methodName, ActionData<Rolegp> actionData) throws BusinessException {
        Class<? extends RolegpBslServiceImpl> clz = this.getClass();
        Method method;
        try {
            method = clz.getMethod(methodName, ActionData.class);
            return (ResultInfo<?>) method.invoke(this, actionData);
        } catch (NoSuchMethodException ex) {
            throw new BusinessException("999999", "没有找到该方法(" + methodName + ")", ex);
        } catch (IllegalAccessException ex) {
            throw new BusinessException("999999", "没有访问该方法权限(" + methodName + ")", ex);
        } catch (InvocationTargetException ex) {
            Throwable targetException = ex.getTargetException();
            if (targetException instanceof BusinessException) {
                BusinessException bex = (BusinessException) targetException;
                throw new BusinessException("999999", bex.getErrorMsg(), ex);
            } else {
                throw new BusinessException("999999", "出错了请重试：(" + methodName + ")---" + ex.getMessage(), ex);
            }
        }
    }

    /**
     * 添加模型
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<Rolegp> add(ActionData<Rolegp> actionData) {
        List<Rolegp> rolegpList = actionData.getData();
        Rolegp rolegp = rolegpList.get(0);
        boolean b = rolegpServiceImpl.save(rolegp);
        if (b) {
            return JsonResult.success(rolegp);
        } else {
            return JsonResult.error(StatusCode.ERROR_ADD);
        }
    }


    /**
     * 批量添加
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<List<Rolegp>> batchAdd(ActionData<Rolegp> actionData) {
        List<Rolegp> rolegpList = actionData.getData();
        boolean b = rolegpServiceImpl.saveBatch(rolegpList);
        if (b) {
            return JsonResult.success(rolegpList);
        } else {
            return JsonResult.error(StatusCode.ERROR_ADD);
        }
    }

    /**
     * 更新
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<Rolegp> update(ActionData<Rolegp> actionData) {
        List<Rolegp> rolegpList = actionData.getData();
        Rolegp component = rolegpList.get(0);
        boolean b = rolegpServiceImpl.updateById(component);
        if (b) {
            return JsonResult.success(component);
        } else {
            return JsonResult.error(StatusCode.ERROR_UPDATE);
        }
    }

    /**
     * 批量更新
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<List<Rolegp>> batchUpdate(ActionData<Rolegp> actionData) {
        List<Rolegp> rolegpList = actionData.getData();
        boolean b = rolegpServiceImpl.updateBatchById(rolegpList);
        if (b) {
            return JsonResult.success(rolegpList);
        } else {
            return JsonResult.error(StatusCode.ERROR_UPDATE);
        }
    }

    /**
     * 删除
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<Integer> delete(ActionData<Rolegp> actionData) {
        return this.batchDelete(actionData);
    }

    /**
     * 批量删除
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<Integer> batchDelete(ActionData<Rolegp> actionData) {
        if (!this.validActionData(actionData)) {
            return JsonResult.error(StatusCode.ERROR_INVALID_DELETE);
        }
        QueryWrapper<Rolegp> queryWrapper = this.buildQueryWrapper(actionData, false);
        int delCount = rolegpServiceImpl.getBaseMapper().delete(queryWrapper);
        if (delCount >= 1) {
            return JsonResult.success(delCount);
        } else {
            return JsonResult.error(StatusCode.ERROR_DELETE);
        }
    }

    /**
     * 根据id进行删除
     *
     * @param actionData 参数
     * @return 返回结果
     */
    @Override
    public ResultInfo<Integer> deleteById(ActionData<Rolegp> actionData) {
        Long id = actionData.getId();
        int delCount = rolegpServiceImpl.getBaseMapper().deleteById(id);
        if (delCount >= 1) {
            return JsonResult.success(delCount);
        } else {
            return JsonResult.error(StatusCode.ERROR_DELETE);
        }
    }

    /**
     * 根据id数组进行删除
     *
     * @param actionData 参数
     * @return 返回结果
     */
    @Override
    public ResultInfo<Integer> deleteByIds(ActionData<Rolegp> actionData) {
        List<Long> ids = actionData.getIds();
        if (ids == null || ids.size() < 1) {
            return JsonResult.error(StatusCode.ERROR_INVALID_DELETE);
        }
        int delCount = rolegpServiceImpl.getBaseMapper().deleteBatchIds(ids);
        if (delCount >= 1) {
            return JsonResult.success(delCount);
        } else {
            return JsonResult.error(StatusCode.ERROR_DELETE);
        }
    }

    /**
     * 不会页查询
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<List<Rolegp>> query(ActionData<Rolegp> actionData) {
        QueryWrapper<Rolegp> queryWrapper = this.buildQueryWrapper(actionData, false);
        List<Rolegp> list = rolegpServiceImpl.list(queryWrapper);
        return JsonResult.success(list);
    }

    /**
     * 根据id数组进行简单查询
     *
     * @param actionData 参数
     * @return 返回值
     */
    @Override
    public ResultInfo<List<Rolegp>> queryByIds(ActionData<Rolegp> actionData) throws BusinessException {
        List<Long> ids = actionData.getIds();
        List<Rolegp> list = rolegpServiceImpl.listByIds(ids);
        return JsonResult.success(list);
    }

    /**
     * 分页查询
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<PageInfo> queryByPage(ActionData<Rolegp> actionData) {
        Page<Rolegp> pageInfo = new Page<>(actionData.getPageNumber(), actionData.getPageSize());
        Page<Rolegp> queryPage = rolegpServiceImpl.page(pageInfo, this.buildQueryWrapper(actionData, false));
        PageInfo pageResult = PageInfo.of(queryPage);
        return JsonResult.success(pageResult);
    }

    /**
     * 分页复杂查询
     *
     * @param actionData 接收参数
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public ResultInfo<PageInfo> complexQuery(ActionData<Rolegp> actionData) {
        QueryWrapper<Rolegp> componentQueryWrapper = this.buildQueryWrapper(actionData, false);
        Page<Rolegp> pageInfo = new Page<>(actionData.getPageNumber(), actionData.getPageSize());
        Page<Rolegp> queryPage = rolegpServiceImpl.page(pageInfo, componentQueryWrapper);
        PageInfo pageResult = PageInfo.of(queryPage);
        return JsonResult.success(pageResult);
    }
}
